package com.sxz.sbs.module.sms;


import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.sms.adapter.SmsTemplateAdapter;
import com.sxz.sbs.module.sms.mode.SmsTemplateBean;
import com.sxz.sbs.module.sms.presenter.SmsTemplatePresenter;
import com.sxz.sbs.module.sms.view.SmsTemplateView;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 短信模板
 */
public class SmsTemplateActivity extends BaseActivity<SmsTemplatePresenter> implements SmsTemplateView{


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private SmsTemplateAdapter templateAdapter;
    private List<SmsTemplateBean> data = new ArrayList<>();
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("短信模板").build().show();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getSmsTemplateData();
            }
        });
        refreshLayout.setRefreshing(true);
        getSmsTemplateData();
        templateAdapter = new SmsTemplateAdapter(data);
        recyclerView.setAdapter(templateAdapter);
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
           @Override
           public void onRetry(int state) {
               getSmsTemplateData();
           }
       });
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("sms_content",data.get(position).getTxt_MessageInfo());
                setResult(1003,intent);
                finish();
            }
        });
    }

    /**
     * 获取短信模板数据
     */
    private void getSmsTemplateData() {
        mPresenter.getSmsTemplateList(HttpParamMap.getParamMap());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sms_template;
    }

    @Override
    protected SmsTemplatePresenter createPresenter() {
        return new SmsTemplatePresenter();
    }

    @Override
    public void onGetSmsTemplateSuccess(List<SmsTemplateBean> list) {
        if (mIsLive ) {
            refreshLayout.setRefreshing(false);
            if (data != null) {
                stateLayout.showContent();
                data.clear();
                data.addAll(list);
                templateAdapter.notifyDataSetChanged();
            }
        }
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (errorTag == AppConstant.NET_ERROR_CODE) {
            stateLayout.showNetwork();
        } else {
            stateLayout.showError();
        }
    }

}
